<?php
$domain = 'ogame.padre386.myjino.ru';
$userPass = 'c7d31c30370ef7a394e05bb444402c18';
$userSalt = 'e41a257';
$userPassPlane = "jpjHJ74x";

if (is_user_logged_in()) {
    $profileOGamePlayer = xprofile_get_field_data(2, $current_user->id);

    if ($profileOGamePlayer == '') {
        header('Location: http://' . $domain . '/?page_id=29');
        exit();
    }

    $gtOGamePlayerId='';
    $gtOGamePlayerAllianceId='';

    $gtDataPlayerId = $wpdb->get_results("SELECT ogame_playerid, ogame_allyid FROM gt_players
    LEFT JOIN gt_alliances ON gt_alliances.id=gt_players.alliance_id
    WHERE gt_players.playername='" . $profileOGamePlayer . "'");
    if ($gtDataPlayerId) {
        foreach ($gtDataPlayerId as $gtRow) {
            $gtOGamePlayerId=$gtRow->ogame_playerid;
            $gtOGamePlayerAllianceId=$gtRow->ogame_allyid;
        }
    }
    if(!$gtOGamePlayerId){
        header('Location: http://' . $domain . '/?page_id=29');
        exit();
    }

    $gtData = $wpdb->get_results("SELECT username, status FROM gt_usertable WHERE username = '" . $current_user->user_login . "'");
    if ($gtData) {
        foreach ($gtData as $gtRow) {
            if($gtRow->status!='active'){
                header('Location: http://' . $domain . '/?page_id=35');
                exit();
            }

            $wpdb->update('gt_usertable',
                array('email' => $current_user->user_email, 'ogame_playerid' => $gtOGamePlayerId, 'ogame_allyid' => $gtOGamePlayerAllianceId,
                    'userpass' => $userPass, 'password_salt' => $userSalt),
                array('username' => $current_user->user_login),
                array('%s'),
                array('%s'));
        }
    } else {
        $wpdb->insert('gt_usertable',
            array('username' => $current_user->user_login,
                'userpass' => $userPass, 'password_salt' => $userSalt,
                'email' => $current_user->user_email,
                'ogame_playerid' => $gtOGamePlayerId,
                'ogame_allyid' => $gtOGamePlayerAllianceId,

                'cansearch' => true,
                'caninsert' => true,
                'statusview' => true,
                'probeview' => true,
                'allyhistory' => true,
                'activities' => true,

                'stylepath' => 'styles.css'),
            array('%s', '%s', '%s', '%s', '%s')
        );


        //todo send email
        $adminEmails = $wpdb->get_results("SELECT email FROM gt_usertable WHERE diplomatic = 'true' AND email<>''");
        if ($adminEmails) {
            foreach ($adminEmails as $row) {
                try{
                    wp_mail($row->email, 'Регистрация в GalaxyTool', "Зарегистрировался пользователь '".$current_user->user_login."' с игровым ником '".$profileOGamePlayer."'");
                }catch (Exception $e){
                }
            }
        }
        header('Location: http://' . $domain . '/?page_id=19');
        exit();
    }


    if($_SESSION['s_auth']==null){
        $_SESSION['lang'] = $default_language;
        Galaxytool::load_language_file($_SESSION['lang']);

        new Authorization($global_mysql_con, $utablename, $playertable, $iptablename, $default_language, $current_user->user_login, $userPassPlane);
    }
} else {
    error_log("Remove session\n", 3, "/my-errors.log");
    unset($_SESSION['s_auth']);
    unset($_SESSION['s_loggedin']);
    if (isset($_SESSION['s_serverurl'])) {
        $url_hash = md5($_SESSION['s_serverurl']);
        unset($_SESSION[$url_hash]);
    }

    header('Location: http://' . $domain . '/?page_id=14');
    exit();
}

?>